#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QImage>
#include <QFile>

#define EXPOSURE "/exposure"
#define GAIN     "/agin"


extern XI_IMG image;


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    loadStyleSheet("Style");

    systemInfo = new SystemInfo;
    connect(systemInfo,SIGNAL(CPUUsageSignal(double)),this,SLOT(CPUUsageSlot(double)));
    connect(systemInfo,SIGNAL(MEMUsageSignal(double)),this,SLOT(MEMUsageSlot(double)));
    connect(systemInfo,SIGNAL(TimeSignal(QString)),this,SLOT(TimeSlot(QString)));

    gpio = new Gpio;

    dvp0 = new Dvpcam;

    qtimer = new QTimer();
    qtimer->setInterval(10000);
    connect(qtimer,SIGNAL(timeout()),this,SLOT(timerOutSlot()));

    int exp = 1500;
    double gain = 1.0;

    settings = new QSettings("Settings.ini");
    if(!settings->value(EXPOSURE).isNull())
    {
        exp = settings->value(EXPOSURE).toInt();
        ui->exposure_spinBox->setValue(exp);
        ui->exposure_horizontalSlider->setValue(exp);
    }
    if(!settings->value(GAIN).isNull())
    {
        gain = settings->value(GAIN).toDouble();
        ui->gain_doubleSpinBox->setValue(gain);
        ui->gain_horizontalSlider->setValue(gain*10);
    }


#if VIDEO == 0
    ui->image_label->hide();
#endif

    _ximea = new Ximea(exp,gain,this);
    _ximea->start();
    connect(_ximea,SIGNAL(readImageSignal()),this,SLOT(readImageSlot()));
    connect(_ximea,SIGNAL(deviceTypeSignal(QString)),this,SLOT(deviceTypeSlot(QString)));
    connect(this,SIGNAL(exposureSignal(int)),_ximea,SLOT(exposureSlot(int)));
    connect(this,SIGNAL(gainSignal(double)),_ximea,SLOT(gainSlot(double)));
    connect(this,SIGNAL(shotSignal()),_ximea,SLOT(shotSlot()));
    connect(gpio,SIGNAL(shotSignal()), _ximea,SLOT(shotSlot()));
    connect(gpio,SIGNAL(shotSignal()), dvp0,SLOT(dvpShotSlot()));
    connect(_ximea,SIGNAL(ximeastartSignal()), gpio, SLOT(cameraReadySlot()));
    connect(dvp0,SIGNAL(dvpokSignal()), gpio, SLOT(dvpDoneSlot()));
    connect(_ximea,SIGNAL(ximeaokSignal()), gpio, SLOT(ximeaDoneSlot()));
}

MainWindow::~MainWindow()
{
    delete dvp0;
    delete settings;
    delete systemInfo;
    delete _ximea;
    delete gpio;
    delete qtimer;
    delete ui;
}


void MainWindow::readImageSlot()
{
    QImage qImage=QImage((unsigned char *)image.bp,image.width,image.height,QImage::Format_Indexed8);
    ui->image_label->setPixmap(QPixmap::fromImage(qImage).scaled(512,272));
    ui->image_label->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
}

void MainWindow::loadStyleSheet(QString name)
{
    QFile file(QString(":/res/%1.css").arg(name));
    file.open(QFile::ReadOnly);
    QString qss = QLatin1String(file.readAll());
    qApp->setStyleSheet(qss);
}

void MainWindow::on_exposure_spinBox_valueChanged(int arg1)
{
    ui->exposure_horizontalSlider->setValue(arg1);
    settings->setValue(EXPOSURE,arg1);
    emit exposureSignal(arg1);
}

void MainWindow::on_gain_doubleSpinBox_valueChanged(double arg1)
{
    ui->gain_horizontalSlider->setValue(arg1*10.0);
    settings->setValue(GAIN,arg1);
    emit gainSignal(arg1);
}

void MainWindow::on_exposure_horizontalSlider_valueChanged(int value)
{
    ui->exposure_spinBox->setValue(value);
    settings->setValue(EXPOSURE,value);
    emit exposureSignal(value);
}

void MainWindow::on_gain_horizontalSlider_valueChanged(int value)
{
    double buf = value/10.0;
    ui->gain_doubleSpinBox->setValue(buf);
    settings->setValue(GAIN,buf);
    emit gainSignal(buf);
}


void MainWindow::on_shoot_pushButton_clicked()
{
    emit shotSignal();
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::RightButton)
    {
        emit shotSignal();
    }
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Q)
    {
        emit shotSignal();
    }
}



void MainWindow::CPUUsageSlot(double u)
{
    QString str="CPU:";
    str.append(QString::number(u,'g',3));
    str.append("%");

    ui->cpu_label->setText(str);
}

void MainWindow::MEMUsageSlot(double u)
{
    QString str="Mem:";
    str.append(QString::number(u,'g',3));
    str.append("%");

    ui->mem_label->setText(str);
}
void MainWindow::TimeSlot(QString t)
{
    ui->time_label->setText(t);
}

void MainWindow::deviceTypeSlot(QString s)
{
    QString str="Camera:";
    str.append(s);
    str.append(" connect");
    disconnect(systemInfo,SIGNAL(TimeSignal(QString)),this,SLOT(TimeSlot(QString)));
    ui->time_label->setText(str);
    qtimer->start();

}

void MainWindow::timerOutSlot()
{
    qtimer->stop();
    connect(systemInfo,SIGNAL(TimeSignal(QString)),this,SLOT(TimeSlot(QString)));
}


